package com.nw.utils;

import java.util.HashMap;
import java.util.Map;

public class StringUtil {
    public static boolean isEmpty(Object param) {
        return param == null || param.equals("") ? true : false;
    }
    public static Map<String,Object> stringToMap(String param) {
        Map map = new HashMap();
        String str = "";
        String key = "";
        Object value = "";
        char[] charList = param.toCharArray();
        boolean valueBegin = false;
        for (int i = 0; i < charList.length; i++) {
            char c = charList[i];
            if (c == '{') {
                if (valueBegin == true) {
                    value = stringToMap(param.substring(i, param.length()));
                    i = param.indexOf('}', i) + 1;
                    map.put(key, value);
                }
            } else if (c == '=') {
                valueBegin = true;
                key = str;
                str = "";
            } else if (c == ',') {
                valueBegin = false;
                value = str;
                str = "";
                map.put(key, value);
            } else if (c == '}') {
                if (str != "") {
                    value = str;
                }
                map.put(key, value);
                return map;
            } else if (c != ' ') {
                str += c;
            }
        }
        return map;
    }
    public static void testFun() {
        String str1 = "{idCard=123, phonenum=1234}";
        String str2 = "{idCard=123, phonenum=1234, map={hhaha=haha}}";
        String str3 = "{idCard=123, phonenum=1234, map={hhaha=haha}, nn={en=ha}}";
        String str4 = "{nn={en=ha}, idCard=123, phonenum=1234, map={hhaha=ni, danshi={ke=shi}}}";
        Map<String, Object> mapresutl1 = (Map<String, Object>) stringToMap(str1);
        Map<String, Object> mapresutl2 = (Map<String, Object>) stringToMap(str2);
        Map<String, Object> mapresutl3 = (Map<String, Object>) stringToMap(str3);
        Map<String, Object> mapresutl4 = (Map<String, Object>) stringToMap(str4);
        System.out.println(mapresutl1.toString());
        System.out.println(mapresutl2.toString());
        System.out.println(mapresutl3.toString());
        System.out.println(mapresutl4.toString());
    }
    public static void main(String[] args) throws Exception {

        StringUtil.testFun();

    }
}
